/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class Definition implements IElement {

    public enum Kind { M, V, R, K, G,}

    public static final String NAME = "Definition";

    private INode parentNode;
    private Kind kind = null;
    private Boolean redefinition = null;
    private Boolean expandable = null;
    private Integer nr = null;
    private Integer line = null;
    private Integer col = null;
    private Integer vid = null;

    private LinkedList<CorrectnessCondition> correctnessCondition1List = new LinkedList<CorrectnessCondition>();
    private Correctness correctness = null;
    private LinkedList<JustifiedProperty> justifiedPropertyList = new LinkedList<JustifiedProperty>();
    private Constructor constructor1 = null;
    private Pattern pattern1 = null;

    private Constructor constructor2 = null;
    private Constructor constructor3 = null;
    private LinkedList<Constructor> constructor4List = new LinkedList<Constructor>();
    private Registration registration = null;
    private LinkedList<CorrectnessCondition> correctnessCondition2List = new LinkedList<CorrectnessCondition>();
    private LinkedList<Pattern> pattern2List = new LinkedList<Pattern>();


    private Definition(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static Definition newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        Definition definition = new Definition(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
            if (xmlAttribute.getName().equals("kind")){
                if (definition.kind == null){
                    if (Kind.valueOf(xmlAttribute.getValue()) != null) {
                        definition.kind = Kind.valueOf(xmlAttribute.getValue());
                    } else 
                        throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("redefinition")){
                if (definition.redefinition == null){
                    try {definition.redefinition = Boolean.parseBoolean(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("expandable")){
                if (definition.expandable == null){
                    try {definition.expandable = Boolean.parseBoolean(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("nr")){
                if (definition.nr == null){
                    try {definition.nr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("line")){
                if (definition.line == null){
                    try {definition.line = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("col")){
                if (definition.col == null){
                    try {definition.col = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("vid")){
                if (definition.vid == null){
                    try {definition.vid = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
                throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (CorrectnessCondition.isCorrectnessCondition(xmlElement.getName())){
                    definition.correctnessCondition1List.add(CorrectnessCondition.newInstance(definition, xmlElementList));
                } else if (xmlElement.getName().equals("Correctness")){
                    if (definition.correctness == null) {
                        definition.correctness = Correctness.newInstance(definition, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Correctness> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("JustifiedProperty")){
                    definition.justifiedPropertyList.add(JustifiedProperty.newInstance(definition, xmlElementList));
                } else if (xmlElement.getName().equals("Constructor") && definition.constructor1 == null){
                    definition.constructor1 = Constructor.newInstance(definition, xmlElementList);
                } else if (xmlElement.getName().equals("Pattern") && definition.pattern1 == null){
                    definition.pattern1 = Pattern.newInstance(definition, xmlElementList);
                } else if (xmlElement.getName().equals("Constructor") && definition.constructor2 == null){
                    definition.constructor2 = Constructor.newInstance(definition, xmlElementList);
                } else if (xmlElement.getName().equals("Constructor") && definition.constructor3 == null){
                    definition.constructor3 = Constructor.newInstance(definition, xmlElementList);
                } else if (xmlElement.getName().equals("Constructor")){
                    definition.constructor4List.add(Constructor.newInstance(definition, xmlElementList));
                } else if (xmlElement.getName().equals("Registration")){
                    if (definition.registration == null) {
                        definition.registration = Registration.newInstance(definition, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Registration> in element " + NAME, xmlElementList);
                    }
                } else if (CorrectnessCondition.isCorrectnessCondition(xmlElement.getName())){
                    definition.correctnessCondition2List.add(CorrectnessCondition.newInstance(definition, xmlElementList));
                } else if (xmlElement.getName().equals("Pattern")){
                    definition.pattern2List.add(Pattern.newInstance(definition, xmlElementList));
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return definition;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.kind != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("kind", this.kind.name());
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.redefinition != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("redefinition", this.redefinition);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.expandable != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("expandable", this.expandable);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.nr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("nr", this.nr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.vid != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("vid", this.vid);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.line != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("line", this.line);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.col != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("col", this.col);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = !(!correctnessCondition1List.isEmpty()
         || correctness != null || !justifiedPropertyList.isEmpty()
         || constructor1 != null || pattern1 != null
         || constructor2 != null || constructor3 != null
         || !constructor4List.isEmpty() || registration != null
         || !correctnessCondition2List.isEmpty() 
         || !pattern2List.isEmpty());
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.constructor1 != null && this.constructor2 != null && this.constructor3 != null){
            xmlElementList.addAll(this.constructor1.getXMLElementList());
            xmlElementList.addAll(this.constructor2.getXMLElementList());
            xmlElementList.addAll(this.constructor3.getXMLElementList());
            for (Constructor constructor : this.constructor4List){
                xmlElementList.addAll(constructor.getXMLElementList());
            }
            if (this.registration != null){
                xmlElementList.addAll(this.registration.getXMLElementList());
            }
            for (CorrectnessCondition correctnessCondition : this.correctnessCondition1List){
                xmlElementList.addAll(correctnessCondition.getXMLElementList());
            }
            if (this.correctness != null){
                xmlElementList.addAll(this.correctness.getXMLElementList());
            }
            if (this.pattern1 != null){
                xmlElementList.addAll(this.pattern1.getXMLElementList());
            }
            for (Pattern pattern : this.pattern2List){
                xmlElementList.addAll(pattern.getXMLElementList());
            }
        } else {
            for (CorrectnessCondition correctnessCondition : this.correctnessCondition1List){
                xmlElementList.addAll(correctnessCondition.getXMLElementList());
            }
            if (this.correctness != null){
                xmlElementList.addAll(this.correctness.getXMLElementList());
            }
            for (JustifiedProperty justifiedProperty : this.justifiedPropertyList){
                xmlElementList.addAll(justifiedProperty.getXMLElementList());
            }
            if (this.constructor1 != null){
                xmlElementList.addAll(this.constructor1.getXMLElementList());
            }
            if (this.pattern1 != null){
                xmlElementList.addAll(this.pattern1.getXMLElementList());
            }
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Kind getKind(){
        return kind;
    }

    public Boolean getRedefinition(){
        return redefinition;
    }

    public Boolean getExpandable(){
        return expandable;
    }

    public Integer getNr(){
        return nr;
    }

    public Integer getLine(){
        return line;
    }

    public Integer getCol(){
        return col;
    }

    public Integer getVid(){
        return vid;
    }

    public LinkedList<CorrectnessCondition> getCorrectnessCondition1List(){
        return correctnessCondition1List;
    }

    public Correctness getCorrectness(){
        return correctness;
    }

    public LinkedList<JustifiedProperty> getJustifiedPropertyList(){
        return justifiedPropertyList;
    }

    public Constructor getConstructor1(){
        return constructor1;
    }

    public Pattern getPattern1(){
        return pattern1;
    }

    public Constructor getConstructor2(){
        return constructor2;
    }

    public Constructor getConstructor3(){
        return constructor3;
    }

    public LinkedList<Constructor> getConstructor4List(){
        return constructor4List;
    }

    public Registration getRegistration(){
        return registration;
    }

    public LinkedList<CorrectnessCondition> getCorrectnessCondition2List(){
        return correctnessCondition2List;
    }

    public LinkedList<Pattern> getPattern2List(){
        return pattern2List;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(CorrectnessCondition correctnessCondition: this.correctnessCondition1List) {
				correctnessCondition.accept(visitor);
			}
			for(JustifiedProperty justifiedProperty: this.justifiedPropertyList) {
				justifiedProperty.accept(visitor);
			}
			for(Constructor constructor: this.constructor4List) {
				constructor.accept(visitor);
			}
			for(CorrectnessCondition correctnessCondition: this.correctnessCondition2List) {
				correctnessCondition.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
